package com.songlanyun.modules.account.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.ObjectUtil;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.*;
import com.songlanyun.modules.account.dao.AccountDao;
import com.songlanyun.modules.account.model.dto.UpdateUserInfoDTO;
import com.songlanyun.modules.exception.MemberException;
import com.songlanyun.modules.wechat.config.WxMaConfiguration;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.songlanyun.modules.account.dao.UserInfoDao;
import com.songlanyun.modules.account.entity.UserInfoEntity;
import com.songlanyun.modules.account.service.UserInfoService;

import javax.annotation.Resource;


@Service("yxtUserInfoService")
public class UserInfoServiceImpl extends ServiceImpl<UserInfoDao, UserInfoEntity> implements UserInfoService {

    @Resource
    private AccountDao accountDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserInfoEntity> page = this.page(
                new Query<UserInfoEntity>().getPage(params),
                new QueryWrapper<UserInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public UserInfoEntity loadById(Long userId, Boolean tw) {
        UserInfoEntity user = this.getById(userId);
        if (user == null && tw) {
            throw new RRException(MemberException.USER_INFO_NOT_EXISTS);
        }
        return user;
    }

    @Override
    public void saveWxInfo(UserInfoEntity userInfo) {

        //保存
        this.saveOrUpdate(userInfo);
    }

    @Override
    public UserInfoEntity getUserInfoByShopId(Long id) {
        return accountDao.getUserInfoByShopId(id);
    }

    @Override
    public String getNameById(Long id) {
        UserInfoEntity userInfoEntity = this.baseMapper.selectById(id);
        if (ObjectUtil.isNotNull(userInfoEntity)) {
            return StringUtils.isNotBlank(userInfoEntity.getRealName()) ? userInfoEntity.getRealName() : userInfoEntity.getNickname();
        }
        return null;
    }


    @Override
    public void updateInfo(Long userId, UpdateUserInfoDTO dto) {
        UserInfoEntity userInfo = this.loadById(userId, true);
        BeanUtil.copyPropertiesIgnoreNull(dto, userInfo);

        this.updateById(userInfo);

    }

    @Override
    public String getResNameById(Long userId) {
        UserInfoEntity userInfoEntity = this.baseMapper.selectById(userId);
        if (ObjectUtil.isNotNull(userInfoEntity)) {
            if (StringUtils.isNotBlank(userInfoEntity.getRealName())) {
                //真实姓名脱敏
                return DesensitizedUtil.chineseName(userInfoEntity.getRealName());
            }
            //昵称无需脱敏
//            else if (StringUtils.isNotBlank(userInfoEntity.getNickname())) {
//                return userInfoEntity.getNickname();
//            }
        }
        return userInfoEntity.getNickname();
    }


}
